package com.jcz.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jcz.biz.Vote_ItemBiz;
import com.jcz.biz.Vote_OptionBiz;
import com.jcz.biz.Vote_SubjectBiz;
import com.jcz.bizImpl.Vote_ItemBizImpl;
import com.jcz.bizImpl.Vote_OptionBizImpl;
import com.jcz.bizImpl.Vote_SubjectBizImpl;
import com.jcz.entity.Vote_Subject;
import com.opensymphony.xwork2.ActionSupport;

import net.sf.json.JSONObject;

public class IndexAction extends ActionSupport {
	private JSONObject obj;/*JSON数据*/
	private Integer page;/*页码*/
	private Integer rows;/*页大小*/
	private String keywords;
	
	public String getKeywords() {
		return keywords;
	}
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	public JSONObject getObj() {
		return obj;
	}
	public void setObj(JSONObject obj) {
		this.obj = obj;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getRows() {
		return rows;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	@Override
	public String execute() throws Exception {
		Vote_SubjectBiz sBiz=new Vote_SubjectBizImpl();
		Vote_ItemBiz iBiz=new Vote_ItemBizImpl();
		Vote_OptionBiz oBiz=new Vote_OptionBizImpl();
		
		List<Vote_Subject> list=sBiz.findAll(keywords, page, rows);
		
		List list2=new ArrayList<>();
		for (Vote_Subject subject : list) {
			Map map=new HashMap<>();
			map.put("id", subject.getVs_id());
			map.put("title", subject.getVs_title());
			map.put("options", oBiz.findCotunt(subject.getVs_id()));/*获取option的count*/
			map.put("participants", iBiz.findCount(subject.getVs_id()));/*获取item的count*/
			list2.add(map);
		}
		Map map2=new HashMap<>();
		map2.put("total", sBiz.findCount());
		map2.put("rows", list2);
		this.setObj(JSONObject.fromObject(map2));
		return SUCCESS;
	}
	
	
}
